﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;
using CE.Configuration;

public partial class Provider_CarInsurance_ProviderParallelImport : System.Web.UI.Page
{
    public static Int64 _providercarparallellimportid = 0;
    string PageName = "CXCI";
    string SubPageName = "CXCIPRLIMPT";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();


    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
             string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
             if (CheckAuthCode(_authcode, 100))
             {
                 CarParallelImport _carparallelimport = new CarParallelImport();
                 CarParallelImportDB _carparallelimportdb = new CarParallelImportDB();

                 _carparallelimport.ProviderCarParallelImportId = CheckCarParallelImport(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToBoolean(ddlParallelImport.Text));
                 int _trantype = 1;
                 if (_carparallelimport.ProviderCarParallelImportId > 0)
                 {
                     _trantype = 2;
                 }
                 _carparallelimport.IsParallelImport = Convert.ToBoolean(ddlParallelImport.Text);
                 _carparallelimport.ParallelImportValue = Convert.ToSingle(txtParallelImportRate.Text.Trim());
                 _carparallelimport.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                 _carparallelimport.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
                 _carparallelimport.Status = true;
                 _carparallelimport.ProviderCarParallelImportId = _carparallelimportdb.Insert(_carparallelimport);

                 if (_carparallelimport.ProviderCarParallelImportId > 0)
                 {
                     InsertProviderAuthTransaction(_carparallelimport.ProviderCarParallelImportId, "CICarParallelImport", _trantype, 100, _authcode, _carparallelimport.ProviderCompanyId, _carparallelimport.RefCEUserId);
                     lblMessage.Text = "Updated successfully!";
                 }
                 else
                 {
                     lblMessage.Text = "Updated failed.";
                 }
             }
             else
             {
                 lblMessage.Text = "Wrong auth code!";
             }
        }
    }

    private void ShowCarParallelImport(Int64 ProviderCompanyId, bool IsParallelImport)
    {
        CarParallelImport _carparallelimport = new CarParallelImport();
        CarParallelImportDB _carparallelimportdb = new CarParallelImportDB();
        _carparallelimport = _carparallelimportdb.GetCarParallelImport(ProviderCompanyId, IsParallelImport);
        if (_carparallelimport != null)
        {
            ddlParallelImport.Text = _carparallelimport.IsParallelImport.ToString();
            txtParallelImportRate.Text = _carparallelimport.ParallelImportValue.ToString();
        }
        else
        {
            txtParallelImportRate.Text = "";
        }
        lblMessage.Text = "";
    }

    private Int64 CheckCarParallelImport(Int64 ProviderCompanyId, bool IsParallelImport)
    {
        CarParallelImport _carparallelimport = new CarParallelImport();
        CarParallelImportDB _carparallelimportdb = new CarParallelImportDB();
        Int64 _retValue = 0;
        _carparallelimport = _carparallelimportdb.GetCarParallelImport(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToBoolean(ddlParallelImport.Text));
        if (_carparallelimport != null)
        {
            _retValue = _carparallelimport.ProviderCarParallelImportId;
        }
        return _retValue;
    }

    protected void ddlParallelImport_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlParallelImport.Text != "-1")
        {
            ShowCarParallelImport(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToBoolean(ddlParallelImport.Text));
        }
        else
        {
            txtParallelImportRate.Text = "";
            lblMessage.Text = "";
        }
    }
    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/CarInsurance/TableMatching.aspx");
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
        }
    }
}
